from ytbase.model.auth import *
from ytbase.model.post import *
from ytbase.model.comment import *
from ytcommunity.blog.setting import *
from ytcommunity.blog.plugin import *
from ytcommunity.blog.model import AUTH_MODEL 

def get(request):
    context = {}
    page = request.GET.get('page', '1')
    context['page'] = int(page)
    context['comment_num_per_page'] = 10
    context['tag'] = request.GET.get('tag', '')
    return context

def request(request):
    context = {}
    context['request'] = request
    return context

def config(request):
    context = {}
    context['config'] = CONFIG
    return context

def auth(request):
    context = {}
    context['auth'] = get_auth((YTPost, None), auth_model=AUTH_MODEL)
    context['auth_comment'] = get_auth((YTComment, None), auth_model=AUTH_MODEL)
    return context

def plugin(request):
    context = {}
    
    plugins = [plugin for plugin in PLUGINS if plugin['type'] == TYPE_SIDE]
    context['plugins'] = get_plugins_context(request, plugins)

    plugins = [plugin for plugin in PLUGINS if plugin['type'] == TYPE_TOP]
    context['plugins_top'] = get_plugins_context(request, plugins)

    plugins = [plugin for plugin in PLUGINS if plugin['type'] == TYPE_BOTTOM]
    context['plugins_bottom'] = get_plugins_context(request, plugins)    

    plugins = [plugin for plugin in PLUGINS if plugin['type'] == TYPE_POST_BOTTOM]
    context['plugins_post_bottom'] = get_plugins_context(request, plugins)    
    
    return context

def get_plugins_context(request, plugins):
    context = []
    for plugin in plugins:
        if plugin.has_key('package'):
            plugin_module = __import__(plugin['package'], globals(), locals(), ['main'], -1)
            data = plugin_module.main(request)
        else:
            data = None
        plugin['view_path'] = plugin['view'] 
        context.append({'config':plugin, 'data':data})
    
    return context  

PROCESSORS = [get, config, auth, plugin]
